home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Sound / WindChime / Source / WindChime.c next >
C/C++ Source or Header  |  1998-06-21  |  6KB  |  190 lines

  1. /*********************************************************/
  2. /*********************************************************/
  3. /****                                                 ****/
  4. /**** WindChime V1.4. © Copyright, 1998, Owen Birnie. ****/
  5. /****                                                 ****/
  6. /**** This program was written using EasySound by     ****/
  7. /**** Anders Bjerin of the Amiga C Club.              ****/
  8. /****                                                 ****/
  9. /**** This program is my own interpretation of an     ****/
  10. /**** identically named program for Windoze machines. ****/
  11. /****                                                 ****/
  12. /**** To Do:-                                         ****/
  13. /**** (*) Add switch to allow the use of one, two or  ****/
  14. /****     four channel audio.                         ****/
  15. /**** (*) Sort out the actual frequencies to use.     ****/
  16. /**** (*) Reduce CPU usage.                           ****/
  17. /**** (*) Turn into a commodity?                      ****/
  18. /****                                                 ****/
  19. /**** History:-                                       ****/
  20. /**** V1.0. Concieved late one night and written the  ****/
  21. /****       next day.                                 ****/
  22. /**** V1.1. Removed bug that meant that it only       ****/
  23. /****       worked when the Wind Chime window was     ****/
  24. /****       active. This had the unfortunate effect   ****/
  25. /****       of turning WindChime into a CPU hog.      ****/
  26. /**** V1.2. Added prop gadgets for volume and         ****/
  27. /****       windiness.                                ****/
  28. /**** V1.3. Added Delay statment into main wait loop  ****/
  29. /****       to reduce CPU usage.                      ****/
  30. /**** V1.4. Added additional sample "Water"           ****/
  31. /****                                                 ****/
  32. /*********************************************************/
  33. /*********************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38. #include <intuition/intuition.h>
  39. #include <graphics/gfxbase.h>
  40. #include <graphics/gfxmacros.h>
  41. #include <libraries/dos.h>
  42. #include <exec/memory.h>
  43.  
  44. #include "EasySound.h"
  45.  
  46. STRPTR version="\0$VER: WindChime V1.4. © Copyright, Owen Birnie, 1998. All Rights Reserved.";
  47. struct  TextAttr default_font=
  48. {
  49.   "topaz.font", TOPAZ_EIGHTY, FS_NORMAL, FPF_ROMFONT
  50. };
  51. struct  IntuiText text=
  52. {
  53.   1, 0, JAM1, 0, 0, &default_font, NULL, NULL
  54. };
  55. struct  IntuiText requester_text=
  56. {
  57.   0, 0, JAM1, 15, 5, &default_font, "PROGRAMMING ERROR!", NULL
  58. };
  59. struct  IntuiText cancel_text=
  60. {
  61.   1, 0, JAM1, 6, 3, &default_font, "OK", NULL
  62. };
  63. /**** Gadgets *********/
  64.  
  65. struct  Image windpropimage;
  66. struct  PropInfo windpropinfo=
  67. {
  68.   FREEHORIZ|AUTOKNOB, 58000, NULL, 1310, NULL, 0, 0, 0, 0, 0, 0
  69. };
  70. struct  Gadget windpropgadget=
  71. {
  72.   NULL, 5, 30, 190, 8, GADGHBOX, RELVERIFY, PROPGADGET, (APTR)&windpropimage,
  73.   NULL, NULL, NULL, (APTR)&windpropinfo, 0, NULL
  74. };
  75. struct  Image volpropimage;
  76. struct  PropInfo volpropinfo=
  77. {
  78.   FREEHORIZ|AUTOKNOB, 40959, NULL, 1213, NULL, 0, 0, 0, 0, 0, 0
  79. };
  80. struct  Gadget volpropgadget=
  81. {
  82.   NULL, 5, 20, 190, 8, GADGHBOX, RELVERIFY, PROPGADGET, (APTR)&volpropimage,
  83.   NULL, NULL, NULL, (APTR)&volpropinfo, 0, NULL
  84. };
  85. /**** End Gadgets *********/
  86. struct  IntuitionBase *IntuitionBase;
  87. struct  GfxBase *GfxBase;
  88. struct  Window *window;
  89. struct  NewWindow new_window=
  90. {
  91.   14, 14, 200, 45, 0, 1, CLOSEWINDOW|GADGETUP, WINDOWCLOSE|WINDOWDRAG|
  92.   WINDOWDEPTH|SMART_REFRESH, NULL, NULL, "Wind Chimes", NULL, NULL,
  93.   200, 15, 200, 15, WBENCHSCREEN
  94. };
  95. CPTR chime;
  96. CPTR water;
  97.  
  98. VOID  clearup (UBYTE stage, STRPTR message)
  99. {
  100.   if (stage&8)
  101.     {
  102.       RemoveSound (chime);
  103.       RemoveSound (water);
  104.     }
  105.   if (stage&4)
  106.     CloseWindow (window);
  107.   if (message!=NULL)
  108.     {
  109.       requester_text.IText=message;
  110.       AutoRequest (NULL, &requester_text, NULL, &cancel_text, NULL, NULL, 320, 72);
  111.     }
  112.   if (stage&2)
  113.     CloseLibrary (IntuitionBase);
  114.   if (stage&1)
  115.     CloseLibrary (GfxBase);
  116.   exit (0);
  117. }
  118.  
  119.  
  120. VOID _main()
  121. {
  122.   BYTE channel=0, volume=volpropinfo.HorizPot/1213;
  123.   UWORD wind=(windpropinfo.HorizPot/1310)+5;
  124.   BOOL finish=FALSE;
  125.   ULONG class;
  126. /*  UWORD code; */
  127.   APTR address;
  128.   
  129.   struct IntuiMessage *message;
  130.   
  131.   if ((IntuitionBase=(struct IntuitionBase *)OpenLibrary ("intuition.library", 0))==NULL)
  132.     exit (!NULL);
  133.   if ((GfxBase=(struct GfxBase *)OpenLibrary ("graphics.library", 0))==NULL)
  134.     clearup (1, "Cannot open \"graphics library\".");
  135.   if ((window=(struct Window *)OpenWindow (&new_window))==NULL)
  136.     clearup (3, "Cannot open window.");
  137.   volpropgadget.TopEdge=window->BorderTop+2;
  138.   windpropgadget.TopEdge=window->BorderTop+12;
  139.   SizeWindow (window, 0, (window->Height-(window->BorderTop+22+window->BorderBottom))*-1);
  140.   AddGadget (window, &volpropgadget, 0);
  141.   AddGadget (window, &windpropgadget, 0);
  142.   RefreshGadgets (&windpropgadget, window, NULL);
  143.   if ((chime=PrepareSound( "Chime" ))==NULL)
  144.     if ((chime=PrepareSound( "PROGDIR:Chime" ))==NULL)
  145.       clearup (7, "Cannot open sample \"Chime\".");
  146.   if ((water=PrepareSound( "Water" ))==NULL)
  147.     if ((water=PrepareSound( "PROGDIR:Water" ))==NULL)
  148.       clearup (7, "Cannot open sample \"Water\".");
  149.   srand (time (NULL));
  150.   while (!finish)
  151.     {
  152.       while ((message=(struct IntuiMessage *)GetMsg (window->UserPort))==NULL)
  153.         {
  154.           if ((rand()%wind)<5)
  155.         {
  156.               PlaySound (chime, volume+10, channel, ((rand()%9)-2)*100, 1);
  157.               channel++;
  158.           if (channel==4)
  159.             channel=0;
  160.         }
  161.           if ((rand()%wind)==0)
  162.         {
  163.               PlaySound (water, volume, channel, 0, 1);
  164.               channel++;
  165.           if (channel==4)
  166.             channel=0;
  167.         }
  168.       Delay (2);
  169.     }
  170.       class=message->Class;
  171. /*      code=message->Code; */
  172.       address=message->IAddress;
  173.       ReplyMsg ((struct Message *)message);
  174.       if (class==CLOSEWINDOW)
  175.     finish=TRUE;
  176.       else if (class==GADGETUP)
  177.     {
  178.       if (address==(APTR)&volpropgadget)
  179.         volume=volpropinfo.HorizPot/1213;
  180.       else if (address==(APTR)&windpropgadget)
  181.         wind=(windpropinfo.HorizPot/1310)+5;
  182.     }
  183.     }
  184.   StopSound (LEFT0);
  185.   StopSound (LEFT1);
  186.   StopSound (RIGHT0);
  187.   StopSound (RIGHT1);
  188.   clearup (15, NULL);
  189. }
  190.